<?php
	require_once('include/lsamp_curl.php');
	$check_form_url = 'http://reports.internic.net/cgi/whois';
	$http_client = new lsamp_cURL(FALSE, '', 'gzip', '', '', 30);
	$tld_str = " .co, .co.uk, .aero, .arpa, .asia,  .biz, .cat, .com, .coop, .edu, .info, .int, .jobs, .mobi, .museum, .name, .net, .org, .pro, .travel";
	$tld_str = str_replace(' ', '',$tld_str);
	$tld_array = explode(',', $tld_str);
	if(isset($_POST['dom_to_check'])){
		$dom = $_POST['dom_to_check'];
		$pos = strpos($dom, '.');
		if($pos !== FALSE){
			$dom = substr($dom, 0, $pos);
		}
		$tld_id = $_POST['tld_id'];
		if($tld_id < count($tld_array)){
			$tld = $tld_array[$tld_id];
			$check_url = $check_form_url
					 . "?whois_nic=" . urlencode($dom . $tld)
					 . "&type=domain";
			$result = $http_client->get($check_url, $errno, $error);
			if($errno != 0){
				echo  "-1|" . $error . "<br>";
				continue;
			}else{
				if(preg_match('/Domain Name: ([^>]+)>/m', $result, $matches)){
					echo ($tld_id + 1) ."|0|" . $dom . $tld . "|not available" ;
					if(preg_match('/Registrar:(.*)/m', $matches[1], $registrars)){
						echo "|" . $registrars[1];
					}else{
						if(preg_match('/Registrant:.*\n(.*)/', $matches[1], $registrars)){
							echo "|" . $registrars[1];
						}else{
							echo "| undefined";
						}
					}

				}else{
					echo ($tld_id + 1) ."|1|" . $dom . $tld . "|available";
				}
			}

		}else{
			echo "-1|Stop";
		}

	}
?>

